<template>
  <div class="edit-dialog">
    <el-dialog
      v-model="dialog"
      :title="titleName"
      :visible.sync="dialog"
      :close-on-click-modal="false"
      :show-close="false"
      width="460px"
      center
    >
      <el-form
        :model="addFormData"
        :rules="rules"
        ref="formRef"
        label-position="right"
        label-width="auto"
      >
        <el-form-item label="机器人列表" prop="deviceList">
          <el-select
            filterable
            v-model="addFormData.deviceList"
            multiple
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in options"
              :key="item.deviceId"
              :label="item.name"
              :value="item.deviceId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button
            type="primary"
            :loading="submitLoading"
            icon="el-icon-check"
            @click="submit('formRef')"
            >提 交</el-button
          >
          <el-button icon="el-icon-close" @click="handleClose">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listAllDevice } from "@/api/deviceManage";
import { addDevice } from "@/api/sceneManage";

export default {
  props: {
    formData: Object,
    showDialog: Boolean,
    editType: Number,
    fieldPatternId:String
  },
  data() {
    return {
      addFormData: {
        fieldPatternId: "",
        deviceList: "",
      },
      titleName: "添加机器人",
      submitLoading: false,
      options: [],
      rules: {
        deviceList: [
          { required: true, message: "请选择机器人", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    listAllDevice().then((res) => {
      this.options = res.value;
    });
  },
  computed: {
    dialog: function () {
      return this.showDialog;
    },
  },
  methods: {
    // 取消事件
    handleClose() {
        this.submitLoading = false;
      this.$emit("changeVisible", false);
    },
    submit(formName) {
    this.addFormData.fieldPatternId = this.fieldPatternId;
      this.submitLoading = true;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const that = this;
          addDevice({...that.addFormData}).then((res) => {
              
              that.submitLoading = false;
              if (res["code"] == 200) {
                that.$emit("changeVisible", false);
                that.$emit("getList");
                that.$message.success("添加成功!");
              } else {
                that.$message(res["msg"]);
              }
            })
            .catch(() => {
              that.submitLoading = false;
            });
        } else {
          that.submitLoading = false;
          return false;
        }
      });
    },
  },
};
</script>

<style></style>
